package org.celery.modules.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.merchant.entity.Merchant;
import org.celery.modules.merchant.vo.MerchantVO;
import org.celery.modules.merchant.wrapper.MerchantWrapper;
import org.celery.modules.merchant.service.IMerchantService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户表. 控制器
 *
 * @author Celery
 * @since 2021-10-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-merchant/merchant")
@Api(value = "商户表.", tags = "商户表.接口")
public class MerchantController extends BladeController {

	private final IMerchantService merchantService;
	private final ITenantService tenantService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入merchant")
	public R<MerchantVO> detail(Merchant merchant) {
		Merchant detail = merchantService.getOne(Condition.getQueryWrapper(merchant));
		return R.data(MerchantWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 商户表.
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入merchant")
	public R<IPage<MerchantVO>> list(Merchant merchant, Query query, BladeUser bladeUser) {
		QueryWrapper<Merchant> queryWrapper = Condition.getQueryWrapper(merchant);
		IPage<Merchant> pages = merchantService.page(Condition.getPage(query), (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Merchant::getTenantId, bladeUser.getTenantId()) : queryWrapper);
		return R.data(MerchantWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 商户表.
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入merchant")
	public R<IPage<MerchantVO>> page(MerchantVO merchant, Query query) {
		IPage<MerchantVO> pages = merchantService.selectMerchantPage(Condition.getPage(query), merchant);
		return R.data(pages);
	}

	/**
	 * 新增 商户表.
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入merchant")
	public R save(@Valid @RequestBody Merchant merchant) {
		return R.status(merchantService.save(merchant));
	}

	/**
	 * 修改 商户表.
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入merchant")
	public R update(@Valid @RequestBody Merchant merchant) {
		return R.status(merchantService.updateById(merchant));
	}

	/**
	 * 新增或修改 商户表.
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入merchant")
	@Transactional
	public R submit(@Valid @RequestBody Merchant merchant) {
		if (Func.isEmpty(merchant.getId())) {
			Tenant tenant = new Tenant() {{
				setAddress(merchant.getAddress());
				setLinkman(merchant.getLinkman());
				setContactNumber(merchant.getContactNumber());
				setTenantName(merchant.getName());
			}};
			if (tenantService.saveTenant(tenant)) {
				Tenant thisTenant = tenantService.getOne(Wrappers.<Tenant>lambdaQuery()
						.eq(Tenant::getAddress, tenant.getAddress())
						.eq(Tenant::getLinkman, tenant.getLinkman())
						.eq(Tenant::getTenantName, tenant.getTenantName())
						.last("limit 1")
				);
				if (Func.isNotEmpty(thisTenant)) {
					merchant.setTenantId(thisTenant.getTenantId());
				}
			}
		} else {
			Tenant tenant = tenantService.getOne(Wrappers.<Tenant>lambdaQuery()
					.eq(Tenant::getTenantId, merchant.getTenantId())
			);
			tenant.setAddress(merchant.getAddress());
			tenant.setLinkman(merchant.getLinkman());
			tenant.setContactNumber(merchant.getContactNumber());
			tenant.setTenantName(merchant.getName());
			tenantService.saveOrUpdate(tenant);
		}
		return R.status(merchantService.saveOrUpdate(merchant));
	}

	
	/**
	 * 删除 商户表.
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Merchant> merchantList = merchantService.list(Wrappers.<Merchant>lambdaQuery()
				.in(Merchant::getId, StringUtil.splitTrim(ids, ","))
		);
		tenantService.removeByIds(merchantList.stream().map(Merchant::getTenantId).collect(Collectors.toList()));
		return R.status(merchantService.deleteLogic(Func.toLongList(ids)));
	}

	
}
